package com.dht.xiao.jianye.command;

import com.dht.xiao.jianye.base.serialport.utils.ByteUtil;
import com.dht.xiao.jianye.base.util.CRC16Utils;

public class CmdData {
    /**
     * 帧头
     */
   static String FRAME_HEAD = "AA55";

    public static String testCmd(String commad,String num){

        StringBuilder chickLock = new StringBuilder();
        chickLock.append(FRAME_HEAD)//帧头
                .append("03")//数据字节数
                .append(commad)//命令标识
                .append(ByteUtil.decimal2fitHex(Integer.valueOf(num)))//开门箱号
                .append("00");//扩展

        String checkCode = CRC16Utils.calcCrc16(chickLock.toString());
        chickLock.append(checkCode);
        return chickLock.toString();
    }
    public static String testCmd1(String commad,String num,String extend){

        StringBuilder chickLock = new StringBuilder();
        chickLock.append(FRAME_HEAD)//帧头
                .append("03")//数据字节数
                .append(commad)//命令标识
                .append(ByteUtil.decimal2fitHex(Integer.valueOf(num)))//开门箱号
                .append(extend);//扩展

        String checkCode = CRC16Utils.calcCrc16(chickLock.toString());
        chickLock.append(checkCode);
        return chickLock.toString();
    }

    /**
     *
     * @param num
     * @param door_type 锁类型
     * @return
     */
    public static String OpenDoor(int num,String door_type){
        String door_num = "00";
        if (door_type.equals("1")){
            door_num = "01";//电磁锁_1 (小麦机器)
        }else if (door_type.equals("2")){
            door_num = "02";//电磁锁_2 (干湿垃圾)
        }
        StringBuilder chickLock = new StringBuilder();
        chickLock.append(FRAME_HEAD)//帧头
                .append("03")//数据字节数
                .append("11")//命令标识
                .append(ByteUtil.decimal2fitHex(num))//开门箱号
                .append(door_num);//扩展

        String checkCode = CRC16Utils.calcCrc16(chickLock.toString());
        chickLock.append(checkCode);
        return chickLock.toString();
    }
    /**
     * 关门
     * @param num
     * @return
     */
    public static String CloseDoor(int num){

        StringBuilder chickLock = new StringBuilder();
        chickLock.append(FRAME_HEAD)//帧头
                .append("03")//数据字节数
                .append("12")//命令标识
                .append(ByteUtil.decimal2fitHex(num))//箱号
                .append("00");//扩展

        String checkCode = CRC16Utils.calcCrc16(chickLock.toString());
        chickLock.append(checkCode);
        return chickLock.toString();
    }
    /**
     * 开光风扇
     * @param num
     * @return
     */
    public static String open_close_Fan(int num,String speed){

        StringBuilder chickLock = new StringBuilder();
        chickLock.append(FRAME_HEAD)//帧头
                .append("03")//数据字节数
                .append("1A")//命令标识
                .append(ByteUtil.decimal2fitHex(num))//箱号
                .append(speed);//速度

        String checkCode = CRC16Utils.calcCrc16(chickLock.toString());
        chickLock.append(checkCode);
        return chickLock.toString();
    }

    /**
     * 去皮
     * @param num
     * @return
     */
    public static String NoWeight(int num){

        StringBuilder chickLock = new StringBuilder();
        chickLock.append(FRAME_HEAD)//帧头
                .append("03")//数据字节数
                .append("14")//命令标识
                .append(ByteUtil.decimal2fitHex(num))//箱号
                .append("00");//扩展

        String checkCode = CRC16Utils.calcCrc16(chickLock.toString());
        chickLock.append(checkCode);
        return chickLock.toString();
    }

    /**
     * 获取重量
     * @param num
     * @return
     */
    public static String GetWeight(int num){

        StringBuilder chickLock = new StringBuilder();
        chickLock.append(FRAME_HEAD)//帧头
                .append("03")//数据字节数
                .append("15")//命令标识
                .append(ByteUtil.decimal2fitHex(num))//箱号
                .append("00");//扩展

        String checkCode = CRC16Utils.calcCrc16(chickLock.toString());
        chickLock.append(checkCode);
        return chickLock.toString();
    }
    /**
     * 查询指定箱体状态
     * @param num
     * @return
     */
    public static String boxState(int num){

        StringBuilder chickLock = new StringBuilder();
        chickLock.append(FRAME_HEAD)//帧头
                .append("03")//数据字节数
                .append("02")//命令标识
                .append(ByteUtil.decimal2fitHex(num))//箱号
                .append("00");//扩展

        String checkCode = CRC16Utils.calcCrc16(chickLock.toString());
        chickLock.append(checkCode);
        return chickLock.toString();
    }

    /**
     * 关门
     * @param num
     * @return
     */
    public static String  bottle(int num){

        StringBuilder chickLock = new StringBuilder();
        chickLock.append(FRAME_HEAD)//帧头
                .append("03")//数据字节数
                .append("1B")//命令标识
                .append(ByteUtil.decimal2fitHex(num))//箱号
                .append("00");//扩展

        String checkCode = CRC16Utils.calcCrc16(chickLock.toString());
        chickLock.append(checkCode);
        return chickLock.toString();
    }

    public static String update_verify(int num,int length,String name){
        int datalength = name.length()+4+1;
        name = ByteUtil.str2HexStr(name);

        StringBuilder chickLock = new StringBuilder();
        chickLock.append(FRAME_HEAD)//帧头
                .append(ByteUtil.integer2HexStr(datalength))//数据字节数
                .append("31")//命令标识
                .append(ByteUtil.decimal2fitHex(num))//箱号
                .append(covering_position(ByteUtil.decimal2fitHex(length),8))//文件大小
                .append(name);//文件名称

        String checkCode = CRC16Utils.calcCrc16(chickLock.toString());
        chickLock.append(checkCode);
        return chickLock.toString();
    }


    /**
     * 数据要求多少字节的，然后长度不够，在前面补0
     * @param s
     * @param len
     * @return
     */
    private static String covering_position(String s,int len){
        if (s.length() < len) {
            int length = s.length();
            for (int i = 0; i < len - length; i++) {
                s = "0"+s;
            }
        }
        return s;
    }
}
